package com.xiaoyung.easyis.app.demo.config;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xiaoyung.easyisapisdk.common.base.Rest;
import com.xiaoyung.easyisapisdk.feign.api.util.CallMethodUtil;

//本地开发用,或者用docker替换云函数环境 时使用
@RestController
@RequestMapping(value="/")
public class AppController {
	protected static final Logger logger = LoggerFactory.getLogger(AppController.class);

	//@Value("${basePackagePath}")
	private String controllerPackagePath = "com.xiaoyung.easyis.app.demo.controller";//.PersonController
	
	/*public String userId;
	public String userName;
	public String nickName;
	public List<String> depts;
	public List<String> roles;*/
	private JSONObject user = JSON.parseObject("{\"userName\":\"zhangsang\",\"nickName\":\"张三\"}"); 
	//private String controllerPackagePath ="Controller";
	

	public static Map<String,Object> buildApiCtx(HttpServletRequest request) {
		Map<String,Object> ctx = new HashMap<>();
		//headers
		Map<String, String> headers = new HashMap<>();
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = headerNames.nextElement();
			String headerValue = request.getHeader(headerName);
			headers.put(headerName, headerValue);
		}
		ctx.put("headers", headers);
		
		
		//query
		Map<String, String> query = new HashMap<>();
		Enumeration<String> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			for (String paramValue : paramValues) {
				query.put(paramName , paramValue);
				//System.out.println(paramName + ": " + paramValue);
			}
		}
		ctx.put("query", query);
		
		ctx.put("method", request.getMethod());

		return ctx;
	}
	//post htpp://localhost:8888/person/add
	//post htpp://localhost:8888/biz/person/add
	@PostMapping(value="/{controller}/{method}") // {controller}要不要带命名空间? xxx/xx/xxController=> xx.xx.xxx
	@ResponseBody
	public Rest<Object> callController(@PathVariable("controller") String controller,@PathVariable("method") String method,@RequestBody JSONObject body, HttpServletRequest request) {
		Map<String,Object> ctx = buildApiCtx(request);
		//ctx.put("url", customUrl);
		ctx.put("body", body);
		ctx.put("user", user);//有可能为空,没token,或者token没有绑定用户
		Rest<Object> ret = CallMethodUtil.callControllerMethod(controllerPackagePath,controller,ctx);
		return ret;
	}
	
}
